/*
 * bytetools.c
 *
 *  Created on: Aug 29, 2016
 *      Author: root
 */
#include "yByteTools.h"

#include <stdint.h>
#include <string.h>
int read_int(char* bytes) {
	union u {
		int32_t x;
		char s[4];
	} U;
	memcpy(U.s, bytes, 4);
	return U.x;
}
int read_short(char* bytes) {
	union u {
		int16_t x;
		char s[2];
	} U;
	memcpy(U.s, bytes, 2);
	return U.x;
}
int read_byte(char* bytes) {

	return bytes[0];
}
float read_float(char* bytes) {
	union u {
		float x;
		char s[4];
	} U;
	memcpy(U.s, bytes, 4);
	return U.x;
}
double read_double(char* bytes) {
	union u {
		double x;
		char s[8];
	} U;
	memcpy(U.s, bytes, 8);
	return U.x;
}
long read_long(char* bytes) {
	union u {
		int64_t x;
		char s[8];
	} U;
	memcpy(U.s, bytes, 8);
	return U.x;
}
void write_int(int value, char* buf) {
	union u {
		int32_t x;
		char s[4];
	} U;
	U.x = value;
	memcpy(buf, U.s, 4);
}
void write_short(short value, char* buf) {
	union u {
		int16_t x;
		char s[2];
	} U;
	U.x = value;
	memcpy(buf, U.s, 2);
}
void write_byte(char value, char* buf) {
	buf[0] = value;
}
void write_float(float value, char* buf) {
	union u {
		float x;
		char s[4];
	} U;
	U.x = value;
	memcpy(buf, U.s, 4);
}
void write_double(double value, char* buf) {
	union u {
		double x;
		char s[8];
	} U;
	U.x = value;
	memcpy(buf, U.s, 8);
}
void write_long(long value, char* buf) {
	union u {
		int64_t x;
		char s[8];
	} U;
	U.x = value;
	memcpy(buf, U.s, 8);
}
